"""
Case Type   : 功能测试
Case Name   : 前置操作:准备数据
Create At   : 2024-07-17
Owner       : qumin
Description :
        1.创建表样并在表中插入数据;expect.成功;
        2.创建表样并在表中插入数据;expect.成功;
Expect      :
        1.成功
        2.成功
History     :
"""
import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger




class CTest0003(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.pri_node = Node('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.target_path = os.path.join(macro.DB_BACKUP_PATH, "source_data")
        self.tab1 = 't_stringfunc_1'
        self.tab2 = 't_stringfunc_2'

    def test1(self):
        text = '-----step1:创建表样并在表中插入数据；expect:成功-----'
        self.log.info(text)
        file_list = ['vecctor_base_tab', 'vecctor_type_tab']
        for file in file_list:
            self.common.scp_file(self.pri_node, f"{file}", self.target_path)
        sql1 = f"""
        drop table if exists {self.tab1}  cascade;
        create table {self.tab1} (
                col_tinyint                  tinyint,
                col_smallint         smallint,
                col_int                      integer,
                col_bigint                   bigint,
                col_numeric                  numeric,
                col_real                     real,
                col_double                   double precision,
                col_decimal          decimal,
                col_varchar          varchar(100),
                col_char                     char(30),
                col_nvarchar2        nvarchar2(100),
                col_text                     text,
                col_timestamptz          date,
                col_timestamp            timestamp without time zone,
                col_date                     date,
                col_time                     varchar(100),
                col_timetz                   varchar(100),
                col_interval         varchar(100),
                col_smalldatetine        varchar(100))
        with (storage_type=ustore) partition by range (col_int)
        (
        partition vecctor_base_tab_000_1 values less than (10),
        partition vecctor_base_tab_000_2 values less than (1357),
        partition vecctor_base_tab_000_3 values less than (2687),
        partition vecctor_base_tab_000_4 values less than (maxvalue));
        copy {self.tab1} from '{os.path.join(self.target_path,
                         'vecctor_base_tab')}' delimiters  ',';"""
        res1 = self.pri_sh.execut_db_sql(sql1)
        self.log.info(res1)
        self.assertIn('COPY 3437', res1, '执行失败' + text)

        text = '-----step2:创建表样并在表中插入数据；expect:成功-----'
        self.log.info(text)
        sql2 = f"""drop table if exists {self.tab2} CASCADE;
        create table {self.tab2} (
                col_tinyint                  tinyint,
                col_smallint         smallint,
                col_int                      integer,
                col_bigint                   bigint,
                col_money            varchar(100),
                col_numeric                  numeric,
                col_real                     real,
                col_double                   double precision,
                col_decimal          decimal,
                col_varchar          varchar(100),
                col_char                     char(30),
                col_nvarchar2        nvarchar2(100),
                col_text                     varchar(500),
                col_timestamp            date,
                col_timestamptz          timestamp without time zone,
                col_date                     date,
                col_time                     varchar(100),
                col_timetz                   varchar(100),
                col_interval         varchar(100),
                col_tinterval        varchar(100),
                col_smalldatetine        varchar(100),
                col_bytea                        varchar(100),
                col_boolean                      boolean,
                col_inet                         varchar(100),
                col_cidr                         varchar(100),
                col_bit                          varchar(100),
                col_varbit                       varchar(100),
                col_oid                          varchar(100)) 
        with (storage_type=ustore);
        copy  {self.tab2} from '{os.path.join(self.target_path,
                            'vecctor_type_tab')}' DELIMITERS  ',';"""
        res2 = self.pri_sh.execut_db_sql(sql2)
        self.log.info(res2)
        self.assertIn('COPY 3621', res2, '执行失败' + text)

    def tearDown(self):
        text = '-----预制数据最后清理-----'
        self.log.info(text)
        self.log.info(f'----{os.path.basename(__file__)} finish----')
